from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()

class OutboundNotice(models.Model):
    """
    出库通知单模型
    - 状态流转：pending → first_approved → second_approved
    - 二级审批人（second_approver）记录最终审批人
    """
    STATUS_CHOICES = (
        ('pending', '待初审'),
        ('first_approved', '初审通过'),
        ('second_approved', '终审通过'),
        ('rejected', '已驳回')
    )
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
    second_approver = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    
    def approve(self, user, is_final=False):
        """
        审批方法
        - is_final: 是否为终审（二级审批）
        - 状态流转逻辑控制
        """
        if self.status == 'pending' and not is_final:
            self.status = 'first_approved'
        elif self.status == 'first_approved' and is_final:
            self.status = 'second_approved'
            self.second_approver = user
        self.save()